/*
public class ListNode {
    int val;
    ListNode next = null;

    ListNode(int val) {
        this.val = val;
    }
}*/

import java.util.Stack;
public class Solution {
    // 递归
    /*
    ListNode ret = new ListNode(0);
    ListNode ret1 = ret;
    public ListNode ReverseList(ListNode head) {
        if (head != null) {
            ReverseList(head.next);
            ret.next =  new ListNode(head.val);
            ret = ret.next;
        }
        return ret1.next;
    }
    */

    // 双链表
    /*
    public ListNode ReverseList(ListNode head) {
        ListNode ret = null;
        while (head != null) {
            ListNode temp = head.next;
            head.next = ret;
            ret = head;
            head = temp;
        }
        return ret;
    }
    */

    // 栈 stack<>() 先进后出
    public ListNode ReverseList(ListNode head) {
        Stack<ListNode> stack = new Stack<>();
        while (head != null) {
            stack.push(head);
            head = head.next;
        }
        if (stack.isEmpty()) {
            return null;
        }
        ListNode ret = new ListNode(-1);
        ListNode dummy = ret;
        while (!stack.isEmpty()) {
            ret.next = stack.pop();
            ret = ret.next;
        }
        ret.next = null;
        return  dummy.next;
    }
}
